Imports BikiSoft.Comm
Imports System.Threading

Namespace Comm
    <ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
    Public Class UsbBikiromInformation
        <ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
        Public Enum _FirmwareVersion As Byte
            FV_CHRIS = 0
            FV_LUCAS = 1
        End Enum
        <ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
        Public Enum _BoardVersion As Byte
            BikiRomUSBV1 = 1
            BikiRomUSBV2 = 2
            BikiRomUSBV3 = 3
            BikiRomUSBV4 = 4
        End Enum
        <ObfuscationAttribute(Exclude:=True, ApplyToMembers:=True)> _
        Public Enum _UserLevel As Integer
            Level8
            Level7
            Level6
            Level5
            Level4
            Level3
            Level2
            Level1
            Unknown = -1
        End Enum

        Private m_Baud As ULong
        Private m_Clock As ULong
        Private m_BoardVersion As _BoardVersion = _BoardVersion.BikiRomUSBV2
        Private m_SerialNo As String = ""
        Private m_BoardNo As UInt32 = 0
        Private m_FirmwareVersion As Byte = 0
        Private m_UserByte As Integer = -1
        Private m_SecByte As Byte = &HFF
        Private m_Init As Boolean = False
        Private m_Comm As Boolean = False
        Private m_consult As Boolean = False
        Private m_libVersion As UInt32
        Private m_dllVersion As UInt32
        Private m_AdcMax As UInt16 = 5120

        Friend Sub Reset()
            m_libVersion = 0
            m_dllVersion = 0
            m_Baud = 0
            m_Clock = 0
            m_SerialNo = 0
            m_BoardNo = 0
            m_UserByte = -1
            m_SecByte = &HFF
            m_Init = False
            m_Comm = False
            m_consult = False
            m_BoardVersion = _BoardVersion.BikiRomUSBV2
        End Sub

#Region "Textual Status Information"
        Public ReadOnly Property InitialisaedStatus() As String
            Get
                If Me.m_Init Then
                    Return "Initialised"
                ElseIf Me.m_Comm Then
                    Return "Unable to Initialise"
                Else
                    Return "Not Connected"
                End If
            End Get
        End Property
        Public ReadOnly Property InitialisedDiagMessage() As String
            Get
                If Me.m_Init Then
                    Return "All OK."
                ElseIf Me.m_Comm Then
                    Return "ECU Must have Power in order to Initialise with the BikiRom USB Board."
                Else
                    Return "BikiRom USB Device not present. Check Device drivers and USB Cable."
                End If
            End Get
        End Property
        Public ReadOnly Property InitialisedPassStatus() As Boolean
            Get
                Return Me.m_Init
            End Get
        End Property

        Public ReadOnly Property CommunicationStatus() As String
            Get
                If Me.m_Comm Then
                    Return "Connected"
                Else
                    Return "Not Connected"
                End If
            End Get
        End Property
        Public ReadOnly Property CommunicationPassStatus() As Boolean
            Get
                Return Me.m_Comm
            End Get
        End Property

        Public ReadOnly Property UserStatus() As String
            Get
                Return UserLevelToString(Me.m_UserByte)
            End Get
        End Property

        Public ReadOnly Property ConsultStatus() As String
            Get
                If Not Me.m_Init Then
                    Return "Unkown"
                ElseIf Me.m_consult Then
                    Return "Initialised"
                ElseIf Me.m_FirmwareVersion < &H1C Then
                    Return "Unknown"
                Else
                    Return "Failed"
                End If
            End Get
        End Property

        Public ReadOnly Property ConsultPassStatus() As Boolean
            Get
                If Me.m_consult Then Return True
                Return False
            End Get
        End Property

        Public ReadOnly Property DriverPassStatus() As Boolean
            Get
                If My.Settings.USBDriverLatestVersion <= Me.m_dllVersion Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property
        Public ReadOnly Property DriverLatestVersion() As String
            Get
                Return String.Format("{0}.{1}.{2}", _
                (My.Settings.USBDriverLatestVersion And &HFF0000UI) / 65536UI, _
                (My.Settings.USBDriverLatestVersion And &HFF00UI) / 256UI, _
                My.Settings.USBDriverLatestVersion And &HFFUI)
            End Get
        End Property
        Public ReadOnly Property DriverCurrentVersion() As String
            Get
                Return String.Format("{0}.{1}.{2}", _
                (Me.m_dllVersion And &HFF0000UI) / 65536UI, _
                (Me.m_dllVersion And &HFF00UI) / 256UI, _
                Me.m_dllVersion And &HFFUI)
            End Get
        End Property

        Public ReadOnly Property LibraryPassStatus() As Boolean
            Get
                If My.Settings.USBLibraryLatestVersion <= Me.m_libVersion Then
                    Return True
                Else
                    Return False
                End If
            End Get
        End Property

        Public ReadOnly Property LibraryLatestVersion() As String
            Get
                Return String.Format("{0}.{1}.{2}", _
                (My.Settings.USBLibraryLatestVersion And &HFF0000UI) / 65536UI, _
                (My.Settings.USBLibraryLatestVersion And &HFF00UI) / 256UI, _
                 My.Settings.USBLibraryLatestVersion And &HFFUI)
            End Get
        End Property

        Public ReadOnly Property LibraryCurrentVersion() As String
            Get
                Return String.Format("{0}.{1}.{2}", _
                (Me.m_libVersion And &HFF0000UI) / 65536UI, _
                (Me.m_libVersion And &HFF00UI) / 256UI, _
                Me.m_libVersion And &HFFUI)
            End Get
        End Property

        Public ReadOnly Property BoardType() As String
            Get
                If m_BoardNo <> 0 Then
                    Return String.Format("{0} (sn: {1})", System.Enum.GetName(GetType(_BoardVersion), Me.m_BoardVersion), m_BoardNo)
                Else
                    Return "Unkown"
                End If
            End Get
        End Property

        Public ReadOnly Property Firmware() As String
            Get
                If Me.m_FirmwareVersion <> 0 Then
                    Return "v" & Hex(Me.m_FirmwareVersion)
                Else
                    Return "Unkown"
                End If
            End Get
        End Property
        Public ReadOnly Property FirmwareLatest() As String
            Get
                Return "v" & Hex(My.Settings.Firmware_Version)
            End Get
        End Property
        Public ReadOnly Property FirmwarePassStatus() As Boolean
            Get
                If ((Me.BoardVersion = _BoardVersion.BikiRomUSBV1 Or Me.BoardVersion = _BoardVersion.BikiRomUSBV2) AndAlso Me.m_FirmwareVersion >= My.Settings.Firmware_Version) _
                Or (Me.BoardVersion = _BoardVersion.BikiRomUSBV3 AndAlso Me.m_FirmwareVersion >= My.Settings.Firmware_Version_33) Then
                    Return True
                End If
                Return False
            End Get
        End Property
#End Region

#Region "Shared Enum to Text conversion"
        Shared Function UserLevelToString(ByVal UserLevel As _UserLevel) As String
            Select Case UserLevel
                Case _UserLevel.Level1
                    Return "Flash and Settings Up/Download Unlocked"
                Case _UserLevel.Level2
                    Return "Flash Upload Locked, Download Unlocked"
                Case _UserLevel.Level3
                    Return "Flash and Settings Upload Locked, Download Unlocked"
                Case _UserLevel.Level4
                    Return "Flash and Settings Up/Download Locked"
                Case _UserLevel.Level5, _UserLevel.Level6, _UserLevel.Level7, _UserLevel.Level8
                    Return "All Locked"
                Case Else
                    Return "Unknown User Security Level"
            End Select
        End Function
#End Region

        Public ReadOnly Property Initialised() As Boolean
            Get
                Return m_Init
            End Get
        End Property
        Public ReadOnly Property Baud() As ULong
            Get
                Return m_Baud
            End Get
        End Property
        Public ReadOnly Property Clock() As ULong
            Get
                Return m_Clock
            End Get
        End Property
        Public ReadOnly Property SerialNo() As String
            Get
                Return m_SerialNo
            End Get
        End Property
        Public ReadOnly Property FirmwareVer() As _FirmwareVersion
            Get
                Return m_FirmwareVersion
            End Get
        End Property
        Public ReadOnly Property BoardNo() As UInt32
            Get
                Return m_BoardNo
            End Get
        End Property
        Public ReadOnly Property OsVersion() As Byte
            Get
                Return m_FirmwareVersion
            End Get
        End Property
        Public ReadOnly Property BaseEnabled() As Boolean
            Get
                If m_UserByte And &H1 Then
                    Return False
                Else
                    Return True
                End If
            End Get
        End Property
        Public ReadOnly Property BoostEnabled() As Boolean
            Get
                If m_UserByte And &H2 Then
                    Return False
                Else
                    Return True
                End If
            End Get
        End Property
        Public ReadOnly Property BoardVersion() As _BoardVersion
            Get
                Return m_BoardVersion
            End Get
        End Property
        Public ReadOnly Property ADCMax() As UInt16
            Get
                Return m_AdcMax
            End Get
        End Property

        Friend Function GetBikiromInfo(ByRef UsbComm As UsbCommunicationHandle) As Boolean
            Dim f_frame As Comm.Comm_Frame
            Dim f_Packet() As Byte = Nothing
            Dim f_cnt As Integer = 0
            Dim bStatus As Boolean = False
            Try
                Reset()

                ' Get static information from the UsbComm Object
                Me.m_Baud = UsbComm.Baud
                Me.m_Comm = UsbComm.IsConnected
                Me.m_Init = UsbComm.Initialised
                Me.m_libVersion = UsbComm.CurrentLibraryVersion
                Me.m_dllVersion = UsbComm.CurrentDriverVersion
                Me.m_BoardVersion = UsbComm.BoardVersion
                Me.m_SerialNo = UsbComm.SerialNumber
                Me.m_BoardNo = 0
                Me.m_Clock = 0
                Me.m_FirmwareVersion = 0
                Me.m_SecByte = &HFF
                Me.m_UserByte = &HFF
                m_AdcMax = 5120

                UsbComm.Timeout = 1000

                Thread.Sleep(100)

                f_frame = New Comm.Comm_Frame(Comm.Comm_Frame.BikiFrameType.ID_RUN_COMMAND, , Comm.Comm_Frame.BikiFrameAction.RUN_GET_INFO, , Comm.Comm_Frame.BikiFrameType.ID_GENERAL_SPECIFIC)
                UsbComm.SendFrame(f_frame, f_Packet, True, 2)

                If Not IsNothing(f_Packet) Then
                    If f_Packet(0) = &HFF Then f_Packet(0) = 0
                    m_BoardNo = (CULng(f_Packet(0)) * 65536UI) + (CULng(f_Packet(1)) * 256UI) + (CULng(f_Packet(2)))
                    m_UserByte = f_Packet(3) And &H7
                    m_SecByte = f_Packet(4)
                    m_Clock = CULng(f_Packet(5))
                    m_FirmwareVersion = f_Packet(6)

                    If m_FirmwareVersion >= &H1C AndAlso f_Packet.Length >= 12 Then
                        Me.m_consult = (f_Packet(11) And 1) Xor 1
                    End If
                    If m_FirmwareVersion >= &H21 AndAlso f_Packet.Length >= 14 Then
                        Me.m_AdcMax = System.BitConverter.ToUInt16(f_Packet, 12)
                    End If
                    Console.WriteLine("MOD_BIKIUSB: Got BikiInfo OK try {0:G}, ADC Max:{1}", f_cnt, m_AdcMax)

                    bStatus = True
                Else
                    Console.WriteLine("MOD_BIKIUSB: Failing getting bikirom info")
                End If
            Catch exUsb As USB_exceptions
                Err_message(exUsb.Description)
            Catch exComm As Comm_Exceptions
                Err_message(exComm.Description)
            Finally

            End Try
            Return bStatus
        End Function

    End Class

End Namespace
